package com.example.banksys;

import DataBase.myDB;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.*;
import javafx.scene.layout.VBox;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

public class PieChartsController {
    public PieChart pieCharts;
    public BarChart barCharts;

    public void initialize() throws SQLException {

        String sql="select type_category,sum(money) sum from trade_record natural join trade_type where account_ID="+QueryPageController.acc_ID+" and DATE_FORMAT(trade_date,'%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m') group by trade_type.type_category";
        myDB database=new myDB("banksystem");
        ResultSet res=database.getStatement().executeQuery(sql);
        ObservableList<PieChart.Data> dataList= FXCollections.observableArrayList();
        while (res.next()) {
            PieChart.Data slice = new PieChart.Data(res.getString("type_category"), Math.abs(res.getDouble("sum")));
            System.out.println("start" + Math.abs(res.getDouble("sum")) + "\n");
            dataList.add(slice);

        }
        pieCharts.setData(dataList);
        pieCharts.setClockwise(true);
        pieCharts.setLabelLineLength(50);
        pieCharts.setLabelLineLength(50);
        pieCharts.setStartAngle(180);

        //下方是柱状图
        sql="select type_category,count(*) count from trade_record natural join trade_type where account_ID="+QueryPageController.acc_ID+" and DATE_FORMAT(trade_date,'%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m') group by trade_type.type_category";
        myDB barDB=new myDB("banksystem");
        ResultSet barres=barDB.getStatement().executeQuery(sql);


        CategoryAxis xAxis = new CategoryAxis();
        xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(
                "save", "take", "loan", "financialProduct","interest")));
        xAxis.setLabel("交易类型");
        //Defining the y axis
        NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("交易次数");

        XYChart.Series<String, Number> series1 = new XYChart.Series<>();
        while(barres.next()){
            series1.getData().add(new XYChart.Data<>(barres.getString("type_category"), barres.getInt("count")));
        }
        barCharts.getData().add(series1);


    }
}
